/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.controller;

import admin.hibernate.model.dao.AdminDonDatHangDAO;
import admin.hibernate.model.dao.AdminKhachHangDAO;
import hibernate.model.dao.ChiTietDonDatHangDAO;
import hibernate.pojo.Chitietdondathang;
import hibernate.pojo.Dondathang;
import hibernate.pojo.Khachhang;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "admincustomercontroller", urlPatterns = {"/admincustomercontroller"})
public class admincustomercontroller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            HttpSession session = request.getSession();
            request.setCharacterEncoding("UTF-8");
            String action = request.getParameter("action");
            if(action!=null)
            {
                //<editor-fold defaultstate="collapsed" desc="Xem danh sách khách hàng">
                if(action.equals("view"))
                {
                    List<Khachhang> lstKhachHang = AdminKhachHangDAO.AdminLayDanhSachKhachHang();
                    if(lstKhachHang!=null)
                    {
                        request.setAttribute("DanhSachKhachHang", lstKhachHang);
                        request.getRequestDispatcher("Admin/pagestile/listcustomer.jsp").forward(request, response);
                    }
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Click vào button chuyển đến trang cập nhật khách hàng">
                if(action.equals("linktoupdate"))
                {
                    String strMaKH = request.getParameter("maKhachHang");
                    int intMaKH = -1;
                    if(strMaKH!=null)
                    {
                        intMaKH = Integer.parseInt(strMaKH);
                    }
                    Khachhang khachhang = AdminKhachHangDAO.AdminLayKhachHangByID(intMaKH);
                    if(khachhang!=null)
                    {
                        request.setAttribute("KhachHangCanCapNhat", khachhang);
                        request.getRequestDispatcher("Admin/pagestile/updatingcustomer.jsp").forward(request, response);
                    }
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Cập nhật khách hàng">
                if(action.equals("update"))
                {
                    String strMaKH = request.getParameter("maKhachHang");
                    int intMaKH = -1;
                    if(strMaKH!=null)
                    {
                        intMaKH = Integer.parseInt(strMaKH);
                    }
                    Khachhang khachhang = AdminKhachHangDAO.AdminLayKhachHangByID(intMaKH);
                    if(khachhang!=null)
                    {
                        String strHoTen = request.getParameter("txt_HoVaTen");
                        String strMatKhau = request.getParameter("txt_MatKhau");
                        String strEmail = request.getParameter("txt_Email");
                        String strGioiTinh = request.getParameter("radio_GioiTinh");
                        String strNgaySinh = request.getParameter("txt_NgaySinh");
                        String strDiaChi = request.getParameter("txt_DiaChi");
                        String strTinhThanhPho = request.getParameter("cbo_TinhThanhPho");
                        String strSoDienThoai = request.getParameter("txt_SoDienThoai");
                        String strNgheNghiep = request.getParameter("cbo_NgheNghiep");
                        String strTinhTrangHonNhan = request.getParameter("cbo_TinhTrangHonNhan");
                        String strCMND = request.getParameter("txt_CMND");
                        String strNgayCap = request.getParameter("txt_NgayCap");
                        String strNoiCap = request.getParameter("txt_NoiCap");
                        String strEmailDuPhong = request.getParameter("txt_EmailDuPhong");
                        String strCauHoiBiMat = request.getParameter("cbo_CauHoiBiMat");
                        String strTraLoiCauHoiBiMat = request.getParameter("txt_TraLoiCauHoiBiMat");
                        String strAnhDaiDien = request.getParameter("txt_AnhDaiDien");
                        String strSoDuTaiKhoan = request.getParameter("txt_SoDuTaiKhoan");
                        
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:00");
                        Date dNgaySinh = null;
                        if(!strNgaySinh.equals(""))
                        {
                            try {
                                dNgaySinh = sdf.parse(strNgaySinh);
                            } catch (ParseException ex) {
                                Logger.getLogger(admincustomercontroller.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        
                        Date dNgayCap = null;
                        if(!strNgayCap.equals(""))
                        {
                            try {
                                dNgayCap = sdf.parse(strNgayCap);
                            } catch (ParseException ex) {
                                Logger.getLogger(admincustomercontroller.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        
                        double dSoDuTaiKhoan = 0;
                        if(!strSoDuTaiKhoan.equals(""))
                        {
                            dSoDuTaiKhoan = Double.parseDouble(strSoDuTaiKhoan);
                        }
                        khachhang.setHoTen(strHoTen);
                        khachhang.setMatKhau(strMatKhau);
                        khachhang.setEmail(strEmail);
                        khachhang.setGioiTinh(strGioiTinh);
                        khachhang.setNgaySinh(dNgaySinh);
                        khachhang.setDiaChi(strDiaChi);
                        khachhang.setThanhPho(strTinhThanhPho);
                        khachhang.setSoDienThoai(strSoDienThoai);
                        khachhang.setNgheNghiep(strNgheNghiep);
                        khachhang.setTinhTrangHonNhan(strTinhTrangHonNhan);
                        khachhang.setCmnd(strCMND);
                        khachhang.setNgayCap(dNgayCap);
                        khachhang.setNoiCap(strNoiCap);
                        khachhang.setEmailDuPhong(strEmailDuPhong);
                        khachhang.setCauHoiBiMat(strCauHoiBiMat);
                        khachhang.setCauTraLoi(strTraLoiCauHoiBiMat);
                        khachhang.setAnhDaiDien(strAnhDaiDien);
                        khachhang.setSoDuTaiKhoan(dSoDuTaiKhoan);
                        boolean flag = AdminKhachHangDAO.AdminCapNhatKhachHang(khachhang);
                        if(flag==true)
                        {
                            request.setAttribute("KhachHangCanCapNhat", khachhang);
                            request.getRequestDispatcher("Admin/pagestile/updatingcustomer.jsp").forward(request, response);
                        }
                        else
                        {
                            out.println("Không cập nhật được.");
                        }
                    }
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Chuyển sang link xem danh sách phiếu mua hàng">
                if(action.equals("viewlistdealbought"))
                {
                     //thông tin mua deal - phiếu mua hàng
                    String maKhachHang = request.getParameter("maKhachHang");
                    List<Chitietdondathang> lstPhieuMuaHang = ChiTietDonDatHangDAO.getOnlineOrderDetail(Integer.parseInt(maKhachHang));
                    request.setAttribute("PhieuMuaHang", lstPhieuMuaHang);
                    request.getRequestDispatcher("Admin/pagestile/listdealbought.jsp").forward(request, response);
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Chuyển sang link xem delivery list">
                if(action.equals("viewlistdelivery"))
                {
                     //thông tin mua deal - phiếu giao hàng
                    String maKhachHang = request.getParameter("maKhachHang");
                    List<Chitietdondathang> lstPhieuGiaoHang = ChiTietDonDatHangDAO.getDeliveryOrderDetail(Integer.parseInt(maKhachHang));
                    request.setAttribute("PhieuGiaoHang", lstPhieuGiaoHang);
                    request.getRequestDispatcher("Admin/pagestile/listdealdelivery.jsp").forward(request, response);
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Chuyển sang link xem historical transacion">
                if(action.equals("viewlisttransactions"))
                {
                    //thông tin mua deal - lịch sử giao dịch
                    String maKhachHang = request.getParameter("maKhachHang");
                    List<Chitietdondathang> lstLichSuGiaoDich = ChiTietDonDatHangDAO.getHistoricalOrderDetail(Integer.parseInt(maKhachHang));
                    request.setAttribute("LichSuGiaoDich", lstLichSuGiaoDich);
                    request.getRequestDispatcher("Admin/pagestile/listdealtransactions.jsp").forward(request, response);
                }
                //</editor-fold>
                
                //<editor-fold defaultstate="collapsed" desc="Confirm order state">
                if(action.equals("confirmorder"))
                {
                     //thông tin mua deal - phiếu giao hàng
                    String maKhachHang = request.getParameter("maKhachHang");
                    Dondathang order = AdminDonDatHangDAO.getOrderByCustomerID(Integer.parseInt(maKhachHang));
                    order.setTinhTrang(1);
                    boolean res = AdminDonDatHangDAO.updateOrderState(order);
                    if(res == true){
                        List<Chitietdondathang> lstPhieuGiaoHang = ChiTietDonDatHangDAO.getDeliveryOrderDetail(Integer.parseInt(maKhachHang));
                        request.setAttribute("PhieuGiaoHang", lstPhieuGiaoHang);
                        request.getRequestDispatcher("Admin/pagestile/listdealdelivery.jsp").forward(request, response);
                    }
                }
                //</editor-fold>
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
